<!-- 店铺管理 StoreMan system:store:list -->
<template>
  <div class="content">
    <div class="content-container">
      <div class="content-btn">
        <Cbutton @click="handleAdd">创建店铺</Cbutton>
      </div>
      <el-table :data="tableList" v-loading="tableLoading" ref="tableData" border fit>
        <el-table-column label="序号" type="index" align="center" width="70"></el-table-column>
        <el-table-column prop="name" label="店铺名称" align="center"></el-table-column>
        <el-table-column prop="scope" label="经营范围" align="center"></el-table-column>
        <el-table-column prop="contactPerson" label="店铺联系人" align="center"></el-table-column>
        <el-table-column prop="contactNumber" label="联系电话" align="center"></el-table-column>
        <el-table-column prop="createTime" label="添加时间" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.createTime | glbTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <!-- active-text="启用" inactive-text="禁用" -->
            <el-switch v-model="scope.row.statusSwitch" @change="handleUpdateStore(scope.row)" active-color="#13ce66"
                       inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <Cbutton type="text" size="medium" @click="handleEdit(scope.row)">编辑</Cbutton>
            <!-- <el-button v-if="scope.row.status === 0" type="text" size="medium" @click="handleUpdateStore(scope.row, 1)">启用</el-button> -->
            <!-- <el-button v-if="scope.row.status === 1" type="text" size="medium" style="color: #F56C6C" @click="handleUpdateStore(scope.row, 0)">禁用</el-button> -->
            <Cbutton type="text" size="medium" color="danger" @click="handleRemove(scope.row)">删除</Cbutton>
          </template>
        </el-table-column>
      </el-table>

      <Pagination @pageChange="getPage" :pageDefultData="{
        pageNum: query.pageNum,
        pageRow: query.pageSize,
        totalCount: totalCount }">
      </Pagination>

      <!-- 创建商铺 修改商铺 -->
      <el-dialog :title="dialog.title" :visible.sync="dialog.mVisible" v-if="dialog.mVisible" width="500px" class="dialog-form">
        <el-form :model="dialog.data" :rules="dialog.rules" ref="DlalogForm" size="medium" label-width="120px">
          <el-form-item label="店铺名称：" prop="name">
            <el-input v-model="dialog.data.name" placeholder="请输入店铺名称"></el-input>
          </el-form-item>
          <el-form-item label="经营范围：" prop="scope">
            <el-input v-model="dialog.data.scope" placeholder="请输入店铺经营范围"></el-input>
          </el-form-item>
          <el-form-item label="店铺联系人：" prop="contactPerson">
            <el-input v-model="dialog.data.contactPerson" placeholder="请输入店铺联系人"></el-input>
          </el-form-item>
          <el-form-item label="联系电话：" prop="contactNumber">
            <el-input v-model="dialog.data.contactNumber" placeholder="请输入联系电话"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="medium" @click="dialog.mVisible = false">取 消</el-button>
          <el-button size="medium" :loading="dialog.loading" type="primary" @click="handleDiaSub('DlalogForm')">确
            定</el-button>
        </span>
      </el-dialog>
      <!-- 创建商铺 修改商铺 -->
    </div>
  </div>
</template>

<script>
import { GetStoreList, UpdateStore, DeleteStore, AddStore } from '@/api/system'
import { ExportList, ExportStr } from '@/utils'
import { StoreInfoRules } from '@/utils/rules'
export default {
  name: '',
  components: {},
  data () {
    return {
      query: {
        keyword: '',
        pageNum: 1,
        pageSize: 10
      },
      totalCount: 0,
      tableList: [],
      tableSelectList: [],
      tableLoading: false,
      storeStatusList: ExportList('storeStatusList'),
      dialog: {
        title: '',
        mVisible: false,
        loading: false,
        oprType: 'ADD',
        data: {
          name: '',
          scope: '',
          contactPerson: '',
          contactNumber: ''
        },
        rules: StoreInfoRules
      }
    }
  },
  props: {},
  watch: {},
  computed: {},
  mounted () {
    this.getList()
  },
  methods: {
    getList () {
      this.tableLoading = true
      let params = JSON.parse(JSON.stringify(this.query))
      GetStoreList(params).then(res => {
        this.tableLoading = false
        let data = res.data
        data.list.forEach(e => {
          // status = 1 启用
          e.statusSwitch = e.status === 1
        })
        this.tableList = data.list
        this.totalCount = data.total
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleUpdateStore (row, type) {
      // status = 1 启用
      type = type || row.status
      let str = ''
      let params = {
        id: row.id,
        name: row.name || '',
        status: ''
      }
      if (type === 1) {
        str = '禁用'
        params.status = '0'
      } else {
        str = '启用'
        params.status = '1'
      }
      this.$confirm(`确定要${str}该商铺吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        UpdateStore(params).then(res => {
          this.getList()
        }).catch(e => { })
      }).catch(() => {
        row.statusSwitch = row.status === 1
      })
    },
    handleRemove (row) {
      this.$confirm(`确定要删除该商铺吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = { id: row.id }
        DeleteStore(params).then(res => {
          this.getList()
        }).catch(e => { })
      }).catch(() => {
        row.statusSwitch = row.status === 1
      })
    },
    handleAdd () {
      this.dialog.title = '创建'
      this.dialog.mVisible = true
      this.dialog.oprType = 'ADD'
      for (const key in this.dialog.data) {
        this.dialog.data[key] = ''
      }
    },
    handleEdit (row) {
      this.dialog.title = '修改'
      this.dialog.mVisible = true
      this.dialog.oprType = 'EDIT'
      this.dialog.data.id = row.id
      for (const key in this.dialog.data) {
        this.dialog.data[key] = row[key]
      }
    },
    handleDiaSub (formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false
        this.dialog.loading = true
        if (this.dialog.oprType === 'ADD') {
          const parmas = JSON.parse(JSON.stringify(this.dialog.data))
          AddStore(parmas).then(res => {
            this.$message.success(res.message || '操作成功')
            this.getList()
            this.dialog.loading = false
            this.dialog.mVisible = false
          }).catch(() => {
            this.dialog.loading = false
          })
        }
        if (this.dialog.oprType === 'EDIT') {
          const parmas = JSON.parse(JSON.stringify(this.dialog.data))
          UpdateStore(parmas).then(res => {
            this.$message.success(res.message || '操作成功')
            this.getList()
            this.dialog.loading = false
            this.dialog.mVisible = false
          }).catch(() => {
            this.dialog.loading = false
          })
        }
      })
    },
    getPage (val) {
      this.query.pageNum = val.pageNum
      this.query.pageSize = val.pageRow
      this.getList()
    },
    dealListStr (key, type) {
      return ExportStr(key, type)
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
